﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.customer.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;


import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.dao.*;
import com.hh.bss.customer.entity.*;
import com.hh.bss.customer.service.*;
import com.hh.bss.customer.vo.query.*;
import com.hh.bss.talent.entity.Cv;
import com.hh.bss.talent.entity.CvContactDetail;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class CompanyCcController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private CompanyCcService companyCcService;
	
	private CompanyCc companyCc;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setCompanyCcServiceImpl(CompanyCcService service) {
		this.companyCcService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,CompanyCcQuery companyCcQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = companyCcService.findPage(companyCcQuery);
		
		List<CompanyCc> CompanyCclist = (List) page.getResult();
		ListRange<CompanyCc> resultList = new ListRange<CompanyCc>();
		resultList.setRows(CompanyCclist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void cclist(HttpServletRequest request,HttpServletResponse response,CompanyCcQuery companyCcQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = companyCcService.findCcPage(companyCcQuery);
		
		List<CompanyCc> CompanyCclist = (List) page.getResult();
		ListRange<CompanyCc> resultList = new ListRange<CompanyCc>();
		resultList.setRows(CompanyCclist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void get(HttpServletRequest request,HttpServletResponse response,CompanyCc companyCc) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			CompanyCc oldCompanyCc = (CompanyCc)companyCcService.getById(companyCc.getId());
                         
			if(oldCompanyCc!=null) result.put("data", oldCompanyCc);
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void projectcccvlist(HttpServletRequest request,HttpServletResponse response,CompanyCcQuery companyCcQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = companyCcService.findProjectCcCvPage(companyCcQuery);
		
		List<CompanyCc> CompanyCclist = (List) page.getResult();
		System.out.println("CompanyCclist:"+CompanyCclist);
		ListRange<CompanyCc> resultList = new ListRange<CompanyCc>();
		resultList.setRows(CompanyCclist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,CompanyCc companyCc) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(CompanyCc.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(companyCc,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(CompanyCc.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(companyCc,"createDt",new Date());}catch(Exception ex){}
				}

			companyCcService.save(companyCc);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void cccvsave(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		CompanyCc companyCc=new CompanyCc();
		Cv cv=new Cv();
		CvContactDetail cvContactDetail=null;
		List contacts=new ArrayList();
		try
		{
			String mobile=request.getParameter("mobile");
			String email=request.getParameter("email");
			String phone=request.getParameter("phone");
			String QQ=request.getParameter("QQ");
			String MSN=request.getParameter("MSN");
			//String salary=request.getParameter("salary");
			bind(request,companyCc);
			bind(request,cv);
			 //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(CompanyCc.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(companyCc,"creator",onlineUser.getUsername());}catch(Exception ex){}
						cv.setNameInformer(onlineUser.getName());
						cv.setNameDt(new Date());
					}
				}
		    if(!StringHelper.isEmpty(mobile)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_MOBILE);
		    	cvContactDetail.setNbr(mobile);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }
		    if(!StringHelper.isEmpty(email)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_EMAIL);
		    	cvContactDetail.setNbr(email);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }
		    if(!StringHelper.isEmpty(phone)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_TEL);
		    	cvContactDetail.setNbr(phone);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }
		    if(!StringHelper.isEmpty(QQ)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_QQ);
		    	cvContactDetail.setNbr(QQ);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }
		    if(!StringHelper.isEmpty(MSN)){
		    	cvContactDetail=new CvContactDetail();		    	
		    	cvContactDetail.setContactType(ConstantDic.ContactType_MSN);
		    	cvContactDetail.setNbr(MSN);
		    	cvContactDetail.setCreateDt(new Date());
		    	if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null) {
						cvContactDetail.setCreator(onlineUser.getUsername());						
					}
				}
		    	contacts.add(cvContactDetail);
		    }
			
                       
							
			companyCc.setCreateDt(new Date());

			companyCcService.saveCcCv(companyCc,cv,contacts);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,CompanyCc companyCc) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        CompanyCc oldcompanyCc = (CompanyCc)companyCcService.getById(companyCc.getId());
                         
			bind(request,oldcompanyCc);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(CompanyCc.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldcompanyCc,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(CompanyCc.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldcompanyCc,"modifiedDt",new Date());}catch(Exception ex){}
				}


			companyCcService.update(oldcompanyCc);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				companyCcService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void addcvcontract(HttpServletRequest request,HttpServletResponse response,CvContactDetail cvContactDetail) throws IOException
	{
		String cvName = request.getParameter("cvName");
		String nbrExt1 = request.getParameter("nbrExt1");
		String nbr = request.getParameter("nbr");
		String nbrExt2 = request.getParameter("nbrExt2");
		String contactType = request.getParameter("contactType");
		
	}
	
}
